`timescale 1ns / 1ps

module Harz_D(
    input [4:0] A1,
    input [4:0] A2,
    input [1:0] Tu1,
    input [1:0] Tu2,
    input [4:0] E_A3,
    input [2:0] E_RinSel,
    input E_RegWr,
    input [1:0] E_Tn,
    input [4:0] M_A3,
    input [2:0] M_RinSel,
    input M_RegWr,
    input [1:0] M_Tn,
    input E_start,
    input E_busy,
    input MDCmd,
    output reg [2:0] D_FA1,
    output reg [2:0] D_FA2,
    output D_Stall
    );

    always @(*) begin
             if(E_A3 == A1 && E_RegWr && !E_Tn && A1 && E_RinSel == 3'b011)
            D_FA1 = 3'b010;
        else if(E_A3 == A1 && E_RegWr && !E_Tn && A1)
            D_FA1 = 3'b001;
        else if(M_A3 == A1 && M_RegWr && !M_Tn && A1 && M_RinSel == 3'b011) 
            D_FA1 = 3'b100;
        else if(M_A3 == A1 && M_RegWr && !M_Tn && A1 && M_RinSel == 3'b010) 
            D_FA1 = 3'b101;
        else if(M_A3 == A1 && M_RegWr && !M_Tn && A1) 
            D_FA1 = 3'b011;
        else D_FA1 = 3'b000;

             if(E_A3 == A2 && E_RegWr && !E_Tn && A2 && E_RinSel == 3'b011)
            D_FA2 = 3'b010;
        else if(E_A3 == A2 && E_RegWr && !E_Tn && A2)
            D_FA2 = 3'b001;
        else if(M_A3 == A2 && M_RegWr && !M_Tn && A2 && M_RinSel == 3'b011) 
            D_FA2 = 3'b100;
        else if(M_A3 == A2 && M_RegWr && !M_Tn && A2 && M_RinSel == 3'b010) 
            D_FA2 = 3'b101;
        else if(M_A3 == A2 && M_RegWr && !M_Tn && A2) 
            D_FA2 = 3'b011;
        else D_FA2 = 3'b000;
    end

    assign D_Stall_A1 = (Tu1 < E_Tn && E_A3 == A1 && E_RegWr && A1) || (Tu1 < M_Tn && M_A3 == A1 && M_RegWr && A1);
    assign D_Stall_A2 = (Tu2 < E_Tn && E_A3 == A2 && E_RegWr && A2) || (Tu2 < M_Tn && M_A3 == A2 && M_RegWr && A2);
    assign D_Stall = D_Stall_A1 || D_Stall_A2 || (MDCmd && (E_busy || E_start));

endmodule
